/**
 * @import libs
 */
import { ref } from 'vue'
import { defineStore } from 'pinia'

export const useGlobalPageStore = defineStore('globalPage', () => {

    /**
     * 主页页面索引
     */
    const indexPageIndex = 0

    /**
     * 搜索页面索引
     */
    const searchPageIndex = 1

    /**
     * 购物页面索引
     */
    const shoppingPageIndex = 2

    /**
     * 分享页面索引
     */
    const sharePageIndex = 3

    /**
     * 宠物医生页面索引
     */
    const petDoctorPageIndex = 4

    /**
     * 底部导航栏下标下限
     */
    const selectedButtomBarIndexMin = 0

    /**
     * 底部导航栏下标上限
     */

    const selectedButtomBarIndexMax = 4
    /**
     * 当前选中的底部导航栏索引
     */
    let selectedButtomBarIndex = ref(4)

    /**
     * 切换底部导航栏下标
     * @param {number} index 
     * @param {string} path
     * @returns {*}
     */
    const changeSelectedButtomBar = (index, path) => {
        
        // 参数有效性校验
        if (index == undefined || path == undefined) {
            return
        }

        // 参数类型校验
        if (typeof index !== 'number') {
            console.error('index must be a number')
            return
        }

        if (typeof path !== 'string') {
            console.error('path must be a string')
            return
        }

        // 参数范围校验
        if (index < selectedButtomBarIndexMin || index > selectedButtomBarIndexMax) {
            console.error('index out of range')
            return
        }

        // 验证是否为当前选中项
        if (selectedButtomBarIndex.value == index) {
            return
        }

        // 无记录跳转页面
        uni.redirectTo({ url: path })

        selectedButtomBarIndex.value = index
    }


    return {
        selectedButtomBarIndex,
        changeSelectedButtomBar
    }

})